#include "StdAfx.h"
#include "user_auth_mgr_impl.h"

namespace aot{ namespace glb{

user_auth_mgr_impl::user_auth_mgr_impl(void)
{
	this->all_view_department_ = 0;
	this->all_view_department_folder_ = 0;
	this->all_send_department_msg_ = 0;
}

user_auth_mgr_impl::~user_auth_mgr_impl(void)
{
}

int
user_auth_mgr_impl::get_view_department( const char* dep_id )
{
	if (dep_id[0] ==0 || dep_id == NULL)
	{
		return false;
	}

	auto_lock_type __tmp_lock(this->mutex_);
	if ( this->all_view_department_ )
	{
		return 1;
	}

	department_auth_map::iterator it = this->dep_auth_map_.find(dep_id);
	if ( it == this->dep_auth_map_.end() )
	{
		return false;
	}

	return it->second.view_department;

}

int
user_auth_mgr_impl::get_view_department_folder( const char* dep_id )
{
	if (dep_id[0] ==0 || dep_id == NULL)
	{
		return false;
	}

	auto_lock_type __tmp_lock(this->mutex_);
	if ( this->all_view_department_folder_ )
	{
		return 1;
	}

	department_auth_map::iterator it = this->dep_auth_map_.find(dep_id);
	if ( it == this->dep_auth_map_.end() )
	{
		return false;
	}

	return it->second.view_department_folder;
}

int
user_auth_mgr_impl::get_send_department_msg( const char* dep_id )
{
	if (dep_id[0] ==0 || dep_id == NULL)
	{
		return false;
	}

	auto_lock_type __tmp_lock(this->mutex_);
	if ( this->all_send_department_msg_ )
	{
		return 1;
	}

	department_auth_map::iterator it = this->dep_auth_map_.find(dep_id);
	if ( it == this->dep_auth_map_.end() )
	{
		return false;
	}

	return it->second.send_department_msg;
}

void user_auth_mgr_impl::set_view_department( const char* dep_id, int val )
{
	if ( dep_id == NULL)
	{
		return;
	}

	auto_lock_type __tmp_lock(this->mutex_);
	if ( dep_id[0] ==0 )
	{
		this->all_view_department_ = 1;
		return;
	}

	department_auth_map::iterator it = this->dep_auth_map_.find(dep_id);
	if ( it == this->dep_auth_map_.end() )
	{
		department_auth_t  auth;
		auth.view_department = val;
		this->dep_auth_map_.insert(std::make_pair(dep_id, auth));
		return;
	}
	it->second.view_department = val;
}

void user_auth_mgr_impl::set_view_department_folder( const char* dep_id, int val )
{
	if ( dep_id == NULL)
	{
		return;
	}

	auto_lock_type __tmp_lock(this->mutex_);
	if ( dep_id[0] ==0 )
	{
		this->all_view_department_folder_ = 1;
		return;
	}

	department_auth_map::iterator it = this->dep_auth_map_.find(dep_id);
	if ( it == this->dep_auth_map_.end() )
	{
		department_auth_t  auth;
		auth.view_department_folder = val;
		this->dep_auth_map_.insert(std::make_pair(dep_id, auth));
		return;
	}
	it->second.view_department_folder = val;

}

void user_auth_mgr_impl::set_send_department_msg( const char* dep_id, int val )
{
	if ( dep_id == NULL)
	{
		return;
	}

	auto_lock_type __tmp_lock(this->mutex_);
	if ( dep_id[0] ==0 )
	{
		this->all_send_department_msg_ = 1;
		return;
	}

	department_auth_map::iterator it = this->dep_auth_map_.find(dep_id);
	if ( it == this->dep_auth_map_.end() )
	{
		department_auth_t  auth;
		auth.send_department_msg = val;
		this->dep_auth_map_.insert(std::make_pair(dep_id, auth));
		return;
	}
	it->second.send_department_msg = val;
}

void 
user_auth_mgr_impl::set_user_auth( const char* auth_key, int auth, const char* param /*= ""*/ )
{
	if (auth_key[0] ==0 || auth_key == NULL)
	{
		return;
	}

	auto_lock_type __tmp_lock(this->mutex_);
	if ( 0 == strcmp(auth_key,"UaceViewDepartment") )
	{
		set_view_department(param, auth);
	}
	else if( 0 == strcmp (auth_key,"UaceViewDepartmentFolder") )
	{
		set_view_department_folder(param, auth);
	}
	else if ( 0 == strcmp (auth_key,"UaceSendDepartmentMessage") )
	{
		set_send_department_msg(param, auth);

	}
	else
	{
		user_auth_map::iterator it = this->user_auth_.find(auth_key);
		if ( it == this->user_auth_.end() )
		{
			this->user_auth_.insert(std::make_pair(auth_key, auth));
		}
		else
		{
			it->second = auth;
		}
	}
}

int 
user_auth_mgr_impl::get_user_auth( const char* auth_key, const char* param /*= ""*/ )
{
	if (auth_key[0] ==0 || auth_key == NULL)
	{
		return false;
	}
	auto_lock_type __tmp_lock(this->mutex_);

	int auth = 0;
	if ( 0 == strcmp(auth_key,"UaceViewDepartment") )
	{
		auth = get_send_department_msg(param);
	}
	else if( 0 == strcmp (auth_key,"UaceViewDepartmentFolder") )
	{
		auth = get_view_department_folder(param);
	}
	else if ( 0 == strcmp (auth_key,"UaceSendDepartmentMessage") )
	{
		auth = get_send_department_msg(param);

	}
	else
	{
		user_auth_map::iterator it = this->user_auth_.find(auth_key);
		if ( it != this->user_auth_.end() )
		{
			auth =  it->second;
		}
	}

	return auth;
}
}}